package com.reborn.mall.api.service.impl;

import com.reborn.mall.api.model.entity.UserReceiveAddress;
import com.reborn.mall.api.service.UserReceiveAddressService;
import org.springframework.stereotype.Service;

/**
 * TODO: Here is description
 *
 * @author Reborn
 * @since 2020/3/20
 */
@Service
public class UserReceiveAddressServiceImpl extends BaseServiceImpl<UserReceiveAddress> implements UserReceiveAddressService {

    @Override
    public UserReceiveAddress insertSelective(UserReceiveAddress entity) {
        if (findAll().size() == 0) {
            entity.setDefaultStatus(1);
        }
        return super.insertSelective(entity);
    }

    @Override
    public int updateByIdSelective(UserReceiveAddress entity) {
        if (entity.getDefaultStatus().equals(1)) {
            UserReceiveAddress qo = new UserReceiveAddress();
            qo.setDefaultStatus(1);
            UserReceiveAddress existedDefaultOne = findOne(qo);
            existedDefaultOne.setDefaultStatus(0);
            super.updateByIdSelective(existedDefaultOne);
        }
        return super.updateByIdSelective(entity);
    }
}
